package com.tide.crm.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import com.tide.crm.dao.ConvergeDictDao;
import com.tide.crm.dao.ConvergeDictDataDao;
import com.tide.crm.model.ConvergeDict;
import com.tide.crm.model.ConvergeDictData;
import com.tide.crm.service.ConvergeDictService;
import org.springframework.stereotype.Service;


@Service("convergeDictService")
public class ConvergeDictServiceImpl implements ConvergeDictService {

	
	@Resource(name="convergeDictDao")
	private ConvergeDictDao convergeDictDao;
	
	@Resource(name="convergeDictDataDao")
	private ConvergeDictDataDao convergeDictDataDao;
	
	@Override
	public Map<String, Object> findDataMap() {
		// TODO Auto-generated method stub
		Map<String, Object> dataMap = new HashMap<String, Object>();
		
		List<ConvergeDict> dictList = convergeDictDao.findDictList();
		List<ConvergeDictData> dictDataList = convergeDictDataDao.findDictDataList();
		//字典定义的全部map
		Map<String, String> dictMap = new HashMap<String, String>();
		
		if(dictList!=null && dictList.size()>0){
			for(ConvergeDict dict:dictList){
				dictMap.put(String.valueOf(dict.getId()), dict.getDictName());
			}
		}
		dataMap.put("dictMap", dictMap);
		
		//处理字典表里的定义的字典
		for(ConvergeDict dict:dictList){
			Map _dictMap = new LinkedHashMap();
			String dictName = dict.getDictName();
			for(ConvergeDictData dictData : dictDataList){
				if(dict.getId().longValue() == dictData.getDictId().longValue()){
					_dictMap.put(dictData.getDataCode(), dictData.getDataName());
				}
			}
			dataMap.put(dict.getDictCode(), _dictMap);
		}
		
		return  dataMap;
	}

	@Override
	public List<Map<String, Object>> findDictListForQuery(Map<String, Object> params) {
		// TODO Auto-generated method stub
		List<ConvergeDict>  dictList = convergeDictDao.findDictListByCodes(params);
		List<Long> dataIdsList = new ArrayList<Long>();
		Map<String,Object> dictIdMap = new HashMap<String,Object>();
		
		Map<String,Object> resultMap = new HashMap<String,Object>();
		
		for(ConvergeDict dict : dictList) {
			dataIdsList.add(dict.getId());
			dictIdMap.put(String.valueOf(dict.getId()), dict.getDictName()+","+dict.getDictCode());
		}
		
		params.put("dictIds", dataIdsList);
		List<ConvergeDictData>  dataList = convergeDictDataDao.findDictDataListByDictIds(params);
		
		for(ConvergeDictData data : dataList) {
			String dictId = String.valueOf(data.getDictId());
			String dictName = String.valueOf(dictIdMap.get(dictId));
			List<Map<String,Object>> list = null;
			Map<String,Object> dataMap =  new HashMap<String,Object>();;
			if(null == resultMap.get(dictName)) {
				list = new ArrayList<Map<String,Object>>();
				dataMap.put("id", data.getId());
				dataMap.put("dataCode", data.getDataCode());
				dataMap.put("name", data.getDataName());
			}else {
				list =(List<Map<String, Object>>) resultMap.get(dictName);
				dataMap.put("id", data.getId());
				dataMap.put("dataCode", data.getDataCode());
				dataMap.put("name", data.getDataName());
			}
			list.add(dataMap);
			resultMap.put(dictName, list);
			
		}
		
		Set<String> resultSet = resultMap.keySet();
		Iterator<String> resultIte = resultSet.iterator();
		
		List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
		while(resultIte.hasNext()) {
			String dictName = String.valueOf(resultIte.next());
			String codes[] = dictName.split(",");
			List<Map<String,Object>> list = (List<Map<String, Object>>) resultMap.get(dictName);
			Map<String,Object> dataMap = new HashMap<String,Object>();
			dataMap.put("title", codes[0]);
			dataMap.put("code", codes[1]);
			dataMap.put("list",list);
			resultList.add(dataMap);
		}
		
		return resultList;
	}

}
